/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.ElevatorDetail;
import org.springblade.modules.admin.entity.MaintainPlanContent;
import org.springblade.modules.admin.entity.MaintainPlanTemplate;
import org.springblade.modules.admin.enums.MaintainPlanContentEnum;
import org.springblade.modules.admin.enums.TaskStatusEnum;
import org.springblade.modules.admin.service.IElevatorDetailService;
import org.springblade.modules.admin.service.IMaintainPlanContentService;
import org.springblade.modules.admin.vo.MaintainPlanTemplateVO;
import org.springblade.modules.admin.mapper.MaintainPlanTemplateMapper;
import org.springblade.modules.admin.service.IMaintainPlanTemplateService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 任务模板关联表 服务实现类
 *
 * @author ysq
 * @since 2022-03-12
 */
@Service
public class MaintainPlanTemplateServiceImpl extends BaseServiceImpl<MaintainPlanTemplateMapper, MaintainPlanTemplate> implements IMaintainPlanTemplateService {

	@Autowired
	private IElevatorDetailService elevatorDetailService;

	@Lazy
	@Autowired
	private IMaintainPlanContentService planContentService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean addPlanTemplate(Long elevatorUseType, Long templateId, String tenantId) {
		List<ElevatorDetail> elevatorDetailList = elevatorDetailService.list(Wrappers.<ElevatorDetail>lambdaQuery().eq(ElevatorDetail::getTenantId,tenantId));
		if(Func.isNotEmpty(elevatorUseType)){
			elevatorDetailList = elevatorDetailService.list(Wrappers.<ElevatorDetail>lambdaQuery().eq(ElevatorDetail::getElevatorUseType,elevatorUseType).eq(ElevatorDetail::getTenantId,tenantId));
		}
		if(Func.isNotEmpty(elevatorDetailList)){
			List<Long> elevatorIds = elevatorDetailList.stream().map(ElevatorDetail::getElevatorId).distinct().collect(Collectors.toList());
			List<List<Long>> elevatorIdsPar = ListUtil.partition(elevatorIds,20);
			elevatorIdsPar.forEach(t -> {
				List<MaintainPlanContent> planContents = planContentService.list(Wrappers.<MaintainPlanContent>lambdaQuery()
					.in(MaintainPlanContent::getElevatorId,t).eq(MaintainPlanContent::getTypeStatus, MaintainPlanContentEnum.MAINTAIN_BY.getId())
					.eq(MaintainPlanContent::getStatus, TaskStatusEnum.BY_DISABLE.getStatus()).eq(MaintainPlanContent::getTenantId,tenantId));
				if(Func.isNotEmpty(planContents)){
					List<Long> planIds = planContents.stream().map(MaintainPlanContent::getId).distinct().collect(Collectors.toList());
					List<MaintainPlanTemplate> old = super.list(Wrappers.<MaintainPlanTemplate>lambdaQuery().in(MaintainPlanTemplate::getPlanId,planIds));
					Map<Long,List<MaintainPlanTemplate>> oldPlanGroup = old.stream().collect(Collectors.groupingBy(MaintainPlanTemplate::getPlanId));
					planIds = planIds.stream().filter(k -> {
						long count = oldPlanGroup.get(k).stream().filter(e -> e.getTemplateId().equals(templateId)).count();
						if(count > 0){
							return Boolean.FALSE;
						}
						return Boolean.TRUE;
					}).collect(Collectors.toList());
					if(Func.isNotEmpty(planIds)){
						List<MaintainPlanTemplate> planTemplates = new ArrayList<>();
						planIds.forEach(e -> {
							MaintainPlanTemplate planTemplate = new MaintainPlanTemplate();
							planTemplate.setPlanId(e);
							planTemplate.setTemplateId(templateId);
							planTemplate.setSort(-9999);
							planTemplates.add(planTemplate);
						});

						List<List<MaintainPlanTemplate>> planTemplatesPar = ListUtil.partition(planTemplates,20);
						planTemplatesPar.forEach(super::saveBatch);
					}
				}
			});
		}
		return Boolean.TRUE;
	}

}
